package org.example.curriculum_design_6_16.controller;

import org.example.curriculum_design_6_16.entity.pojo.CompetitionCatalog;
import org.example.curriculum_design_6_16.entity.vo.Result;
import org.example.curriculum_design_6_16.service.CompetitionCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/competitionCatalog")
public class CompetitionCatalogController {

    @Autowired
    private CompetitionCatalogService competitionCatalogService;

    /**
     * 添加竞赛目录
     * @param competitionCatalog 竞赛目录信息
     * @return 操作结果
     */
    @PostMapping
    public Result addCompetition(@RequestBody CompetitionCatalog competitionCatalog) {
        return Result.success(competitionCatalogService.addCompetition(competitionCatalog));
    }

    /**
     * 更新竞赛目录
     * @param competitionCatalog 竞赛目录信息
     * @return 操作结果
     */
    @PutMapping
    public Result updateCompetition(@RequestBody CompetitionCatalog competitionCatalog) {
        return Result.success(competitionCatalogService.updateCompetition(competitionCatalog));
    }

    /**
     * 删除竞赛目录
     * @param id 竞赛ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result deleteCompetition(@PathVariable Integer id) {
        return Result.success(competitionCatalogService.deleteCompetition(id));
    }

    /**
     * 根据ID获取竞赛目录
     * @param id 竞赛ID
     * @return 竞赛目录信息
     */
    @GetMapping("/{id}")
    public Result getCompetitionById(@PathVariable Integer id) {
        return Result.success(competitionCatalogService.getCompetitionById(id));
    }

    /**
     * 获取所有竞赛目录
     * @return 竞赛目录列表
     */
    @GetMapping
    public Result getAllCompetitions() {
        return Result.success(competitionCatalogService.getAllCompetitions());
    }

    /**
     * 根据级别和类别筛选竞赛目录
     * @param level 竞赛级别
     * @param category 竞赛类别
     * @return 符合条件的竞赛目录列表
     */
    @GetMapping("/filter")
    public Result getCompetitionsByLevelAndCategory(
            @RequestParam String level,
            @RequestParam String category) {
        return Result.success(competitionCatalogService.getCompetitionsByLevelAndCategory(level, category));
    }

    @GetMapping("/getId")
    public Result getId(@RequestParam String name) {  // 改用 @RequestParam
        try {
            CompetitionCatalog catalog = competitionCatalogService.getCompetitionByName(name);
            return Result.success(catalog.getId());
        } catch (Exception e) {
            return Result.error("未找到竞赛: " + name);
        }
    }
}